package com.sqx.modules.errand.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.PageUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.dao.UserDao;
import com.sqx.modules.app.dao.UserMoneyDetailsDao;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.entity.UserMoneyDetails;
import com.sqx.modules.app.service.UserMoneyDetailsService;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.common.entity.CommonInfo;
import com.sqx.modules.common.service.CommonInfoService;
import com.sqx.modules.errand.dao.UserInfoDao;
import com.sqx.modules.errand.entity.*;
import com.sqx.modules.errand.service.IndentService;
import com.sqx.modules.errand.service.UserInfoService;
import com.sqx.modules.tbCoupon.entity.TbCoupon;
import com.sqx.modules.tbCoupon.service.TbCouponService;
import com.sqx.modules.tbCoupon.service.TbCouponUserService;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoDao, Evaluate> implements UserInfoService {

    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private CommonInfoService commonInfoService;
    @Autowired
    private UserService userService;
    @Autowired
    private UserDao userDao;
    @Autowired
    private TbCouponService couponService;
    @Autowired
    private TbCouponUserService couponUserService;
    @Autowired
    private IndentService indentService;
    @Autowired
    private UserMoneyDetailsDao userMoneyDetailsDao;

    @Override
    public UserEntity findUserInfoById(Long userId) {

        return userInfoDao.findUserInfoById(userId);
    }


    @Override
    public Result checkPhone(String openId, String phone, Integer userType) {
        UserEntity userEntity = userService.queryByPhone(phone);
        if (userEntity != null) {
            userService.remove(new QueryWrapper<UserEntity>().eq("open_id", openId));
            userEntity.setOpenId(openId);
            userService.updateById(userEntity);
            return userService.getResult(userEntity);
        } else {
            userEntity = userService.queryByOpenId(openId, userType);
            userEntity.setPhone(phone);
            userService.updateById(userEntity);
            return Result.success();
        }
    }

    @Override
    public Result rankingList(Integer page, Integer limit, String address, String date, String dateType) {
        Page<Indent> pages = new Page<>(page, limit);
        PageUtils pageUtils = new PageUtils(userInfoDao.rankingList(pages, address, date, dateType));
        return Result.success().put("data", pageUtils);
    }

    @Override
    public Result bannedFlag(Long userId) {
        UserEntity userEntity = userDao.selectById(userId);
        if (userEntity != null) {
            return Result.success().put("data", userEntity.getStatus());
        } else {
            return null;
        }
    }

    @Override
    public Result selectCashDeposit(Long userId, Integer page, Integer limit) {
        Page<UserMoneyDetails> pages = new Page<>(page, limit);
        PageUtils pageUtils = new PageUtils(userInfoDao.selectCashDeposit(pages, userId));
        return Result.success().put("data", pageUtils);
    }


    @Override
    public Result updateUserInfoById(UserEntity tbUser) {
        int i = userInfoDao.updateUserInfoById(tbUser.getNickName(), tbUser.getAvatar(), tbUser.getPhone(), tbUser.getCashQrCode(), tbUser.getUserId());
        return Result.success();
    }

    @Override
    public MoneyDetails findAccount(Long userId) {
        return userInfoDao.findAccount(userId);
    }

    @Override
    public Result userFeedback(Long userId, Feedback feedback) {
        userInfoDao.userFeedback(userId, feedback.getUserEmail(), feedback.getFeedbackScore(), feedback.getFeedbackMessage(), new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), feedback.getFeedType());
        return Result.success();
    }

    @Override
    public Result findIncome(Long userId) {
        HashMap<String, Object> hashMap = new HashMap<>();
        int indentCount = indentService.getUserIndentCount(userId);
        BigDecimal indentMoney = indentService.getUserIndentMoney(userId);
        hashMap.put("indentCount", indentCount);
        hashMap.put("indentMoney", indentMoney);


        return Result.success().put("data", hashMap);
    }

    @Override
    public Result findBalance(Long userId) {
        double balance = userInfoDao.findBalance(userId);
        return Result.success().put("data", balance);
    }

    @Override
    public Result findCashDeposit(Long userId) {
        double cashDeposit = userInfoDao.findCashDeposit(userId);
        return Result.success().put("data", cashDeposit);
    }

    @Override
    public IPage<UserMoneyDetails> findMoneyDetails(Long userId, Integer page, Integer limit, UserMoneyDetails userMoneyDetails) {
        Page<UserMoneyDetails> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
            pages.setSize(-1);
        }


        return  userMoneyDetailsDao.findMoneyDetails(pages,userMoneyDetails);
    }

    @Override
    public Result findEvaluate(Long userId, Integer page, Integer limit, String satisfactionFlag) {
        Page<Evaluate> pages = new Page<>(page, limit);
        PageUtils pageUtils = new PageUtils(baseMapper.findEvaluate(pages, userId, satisfactionFlag));
        return Result.success().put("data", pageUtils);
    }

    @Override
    public Result newUserCoupon(Long userId) {
        CommonInfo one = commonInfoService.findOne(253);
        if (one != null) {
            String[] couponInfo = one.getValue().split(",");
            if (couponInfo.length != 2) {
                return Result.error("参数配置错误,请联系管理员");
            }
            //查看该用户是否是新用户
            UserEntity entity = userService.getById(userId);
            if (1 == entity.getNewUserFlag()) {
                TbCoupon tbCoupon = couponService.getById(couponInfo[0]);
                if (tbCoupon == null) {
                    return Result.error("优惠券不存在");
                }
                couponUserService.giveCoupon(tbCoupon, userId);
                //修改用户为非新用户
                entity.setNewUserFlag(2);
                userService.updateById(entity);
                return Result.success();
            }
            return Result.error("本福利仅新用户可领取！");
        } else {
            return Result.error("参数未配置,请联系管理员");
        }

    }


    @Override
    public Result updateOnLine(Long userId, Integer onLineFlag) {
        userInfoDao.updateOnLine(userId, onLineFlag);
        return Result.success();
    }


    public static Date addDate(int num) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, num);
        Date date = calendar.getTime();
        return date;
    }


}
